package com.hxgis.common.utils;

import java.awt.*;

/**
 * @author BLQ
 * @date 2019/8/8 9:38
 */
public class ColorUtil {
    /**
     * 十六进制字符串转Color
     * @param hexStr #FFFFFF
     * @return
     */
    public static Color hexStrToColor(String hexStr) {
        if (hexStr.startsWith("#") && hexStr.length() == 7) {
            int r = Integer.parseInt(hexStr.substring(1, 3), 16);
            int g = Integer.parseInt(hexStr.substring(3, 5), 16);
            int b = Integer.parseInt(hexStr.substring(5, 7), 16);
            return new Color(r, g, b);
        } else {
            return null;
        }
    }

    /**
     * 十六进制字符串转Color
     * @param color
     * @return #FFFFFF
     */
    public static String ColorToHex(Color color) {
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        String red = Integer.toHexString(r).toUpperCase();
        String green = Integer.toHexString(g).toUpperCase();
        String blue = Integer.toHexString(b).toUpperCase();
        return "#" + red + green + blue;
    }
}
